package com.zpyr.mvc.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.mybatis.spring.SqlSessionTemplate;

import com.zpyr.common.StringUtil;
import com.zpyr.mvc.vo.AddrList;
import com.zpyr.mvc.vo.Carc_category_info;
import com.zpyr.mvc.vo.Carc_evaluation;
import com.zpyr.mvc.vo.Carc_info;
import com.zpyr.mvc.vo.Carc_member;
import com.zpyr.mvc.vo.Carc_user_info;

import com.zpyr.mvc.interfaces.CarcMapper;

@Controller
public class CarcDataCont {
	
	@Autowired
	private SqlSessionTemplate sqlSession;
	
	/**
	 * 카센터 목록 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carcenter/data/carc_list.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_list(/*@RequestParam("info_seq")*/ String info_seq , String addr_sido , String addr_gubun , String addr_dong , String name , String category1 , String order_col , String order_method , String dataType) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq" , info_seq);
		parameters.put("addr_sido" , addr_sido);
		parameters.put("addr_gubun" , addr_gubun);
		parameters.put("addr_dong" , addr_dong);
		parameters.put("name" , name);
		parameters.put("category1" , category1);
		if(order_col == null || order_col.equals("") ) order_col = "info_seq";
		parameters.put("order_col" , order_col);
		if(order_method == null || order_method.equals("") ) order_method = "desc";
		parameters.put("order_method" , order_method);
		
		List<Carc_info> carc_list = mapper.getList_carc_info(parameters);
		
		String retStr = "carc/d/carc_list";
		if(dataType.equals("json")) retStr = "carc/d/json/carc_list";
		ModelAndView mav = new ModelAndView(retStr);
		
		mav.addObject("carc_list", carc_list);
		return mav;
	}
	
	/**
	 * 카센터 유저 등록 정보 목록 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carcenter/data/carc_user_list.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_user_list(/*@RequestParam("info_seq")*/ String addr_sido , String addr_gubun , String addr_dong , String name , String order_col , String order_method) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("addr_sido" , addr_sido);
		parameters.put("addr_gubun" , addr_gubun);
		parameters.put("addr_dong" , addr_dong);
		parameters.put("name" , name);
		if(order_col == null || order_col.equals("") ) order_col = "user_info_seq";
		parameters.put("order_col" , order_col);
		if(order_method == null || order_method.equals("") ) order_method = "desc";
		parameters.put("order_method" , order_method);
		parameters.put("order_col" , order_col);
		parameters.put("order_method" , order_method);
		
		List<Carc_user_info> carc_user_list = mapper.getList_carc_user_info(parameters);
		
		ModelAndView mav = new ModelAndView("carc/d/carc_user_list");
		
		mav.addObject("carc_user_list", carc_user_list);
		return mav;
	}
	
	
	/**
	 * 카센터 리스트 보기. 현재위치로부터 주변을 검색
	 */
	@RequestMapping(value="/carcenter/data/carc_list_around.gcm" , params={"!nouse"})
	public ModelAndView searchAroundByDistance(String distance , String lat , String lng , String dataType) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("distance", distance);
		parameters.put("lat", lat);
		parameters.put("lng", lng);
		
		List<Carc_info> retList = mapper.searchAroundByDistance(parameters);
		if(dataType == null || dataType.equals("")) dataType = "xml";
		
		String returnStr = "";
		if(dataType.equals("xml")) returnStr = "carc/d/carc_list_around";
		else if(dataType.equals("json")) returnStr = "carc/d/json/carc_list";
		ModelAndView mav = new ModelAndView(returnStr);
		mav.addObject("carc_list" , retList);
		return mav;		
	}
	
	/**
	 * 카센터 정보 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carc/data/carc_info.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_info(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		Carc_info carc_info = mapper.getCarc_info(info_seq);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq" , info_seq);
		List<Carc_evaluation> eval_list = mapper.getList_carc_evaluation(parameters);
		
		ModelAndView mav = new ModelAndView("carc/carc_info");
		
		mav.addObject("carc_info", carc_info);
		mav.addObject("eval_list", eval_list);
		return mav;
	}

	/**
	 * 카센터 주소로 트리형 구조 가져오기.
	 */
	@RequestMapping(value="/carcenter/data/getAddrTree.gcm" , params={"!nouse"})
	public ModelAndView getAddrTree(/*@RequestParam("info_seq")*/) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		List<AddrList> addr_list = mapper.selectAddrList();
		
		ModelAndView mav = new ModelAndView("carc/d/json/addr_tree");
		
		mav.addObject("addr_list", addr_list);
		return mav;
	}

	/**
	 * 카테고리 리스트 보기.
	 * @param category_gubun
	 */
	@RequestMapping(value="/carcenter/data/category_list.gcm" , params={"!nouse"})
	public ModelAndView getCategory( String category_gubun , String callback ) {
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("category_gubun" , category_gubun);
		List<Carc_category_info> category_list = mapper.selectCategoryList( parameters );
		
		ModelAndView mav = new ModelAndView("carc/d/json/category_list");
		
		mav.addObject("callback", callback);
		mav.addObject("category_list", category_list);
		return mav;
	}
	/**
	 * 회원정보 가져오기
	 * @param info_seq
	 */
	@RequestMapping(value="/carcenter/data/member_info.gcm" , params={"!nouse"})
	public ModelAndView selectMember( String login_email , String use_yn , String dataType , String mem_nickname , String password ) {
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("login_email" , login_email);
		parameters.put("use_yn" , use_yn);
		parameters.put("mem_nickname" , mem_nickname);
		
		Carc_member member = mapper.selectMember( parameters );
		
		String retStr = "carc/d/member";
		if(dataType!=null && dataType.equals("json")) retStr = "carc/d/json/member";
		ModelAndView mav = new ModelAndView(retStr);
		
		mav.addObject("validPwd" , "N");
		String real_pwd = "" , encode_pwd = "";
		if(member!=null) {
			real_pwd = member.getPassword();
			encode_pwd = StringUtil.encodeMD5(password);
			if( real_pwd.equals(encode_pwd) ) {
				mav.addObject("validPwd" , "Y");
			}
		}
		mav.addObject("member", member);
		return mav;
	}

	@RequestMapping(value="/carcenter/data/eval_list.gcm" , params={"!nouse"})
	public ModelAndView viewEval_list(/*@RequestParam("info_seq")*/ String info_seq , String dataType ) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq" , info_seq);
		
		List<Carc_evaluation> eval_list = mapper.getList_eval_info(parameters);
		
		String retStr = "carc/d/eval_list";
		if(dataType.equals("json")) retStr = "carc/d/json/eval_list";
		ModelAndView mav = new ModelAndView(retStr);
		
		mav.addObject("eval_list", eval_list);
		return mav;
	}
	
		
}